package com.warm.base.jdk.thread.base;

/**
 * 军队线程
 * 模拟双方作战的行为
 */
public class ArmyRunnable implements Runnable {

    //volatile保证了线程可以正确的读取其他线程写入的值
    //可见性 ref JMM ，happen-before原则
    volatile boolean keepRuuning = true;

    @Override
    public void run() {

        while (keepRuuning) {
            //发动五连击
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "进攻对方[" + i + "]");
                //让出处理器时间，下次谁进攻还不一定
                Thread.yield();
            }
        }
        System.out.println(Thread.currentThread().getName() + "结束了战斗");
    }
}
